home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / mm / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-09  |  54.3 KB  |  2,095 lines

  1. /*
  2.  *  linux/mm/memory.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file README.legal in the main directory of this archive
  8.  * for more details.
  9.  */
  10.  
  11. /*
  12.  *  Heavily modified 1993 by Hamish Macdonald for MC680X0 support
  13.  *
  14.  *  68040 fixes by Michael Rausch
  15.  *  68040 fixes by Martin Apel
  16.  */
  17.  
  18. /*
  19.  * demand-loading started 01.12.91 - seems it is high on the list of
  20.  * things wanted, and it should be easy to implement. - Linus
  21.  */
  22.  
  23. /*
  24.  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
  25.  * pages started 02.12.91, seems to work. - Linus.
  26.  *
  27.  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
  28.  * would have taken more than the 6M I have free, but it worked well as
  29.  * far as I could see.
  30.  *
  31.  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
  32.  */
  33.  
  34. /*
  35.  * Real VM (paging to/from disk) started 18.12.91. Much more work and
  36.  * thought has to go into this. Oh, well..
  37.  * 19.12.91  -    works, somewhat. Sometimes I get faults, don't know why.
  38.  *        Found it. Everything seems to work now.
  39.  * 20.12.91  -    Ok, making the swap-device changeable like the root.
  40.  */
  41.  
  42. #include <asm/system.h>
  43. #include <asm/segment.h>
  44. #include <linux/config.h>
  45.  
  46. #include <linux/signal.h>
  47. #include <linux/sched.h>
  48. #include <linux/traps.h>
  49. #include <linux/head.h>
  50. #include <linux/kernel.h>
  51. #include <linux/malloc.h>
  52. #include <linux/errno.h>
  53. #include <linux/string.h>
  54. #include <linux/types.h>
  55. #include <linux/bootinfo.h>
  56.  
  57. #include <linux/mm.h>
  58. #include <linux/mman.h>
  59.  
  60. unsigned long high_memory = 0;
  61.  
  62. extern void die_if_kernel(char *,struct frame *,int);
  63.  
  64. int nr_swap_pages = 0;
  65. int nr_free_pages = 0;
  66. unsigned long free_page_list = 0;
  67. /*
  68.  * The secondary free_page_list is used for malloc() etc things that
  69.  * may need pages during interrupts etc. Normal get_free_page() operations
  70.  * don't touch it, so it stays as a kind of "panic-list", that can be
  71.  * accessed when all other mm tricks have failed.
  72.  */
  73. int nr_secondary_pages = 0;
  74. unsigned long secondary_page_list = 0;
  75.  
  76. #define copy_page(from,to) (memcpy((void*)to,(const void *)from,PAGE_SIZE))
  77.  
  78. unsigned short * mem_map = NULL;
  79.  
  80. unsigned long *get_pointer_table (void);
  81. #define get_root_table() get_pointer_table()
  82. void free_pointer_table (unsigned long *);
  83. #define free_root_table(ptr) free_pointer_table((ptr))
  84. unsigned long *get_page_table (unsigned long *);
  85.  
  86. /*
  87.  * oom() prints a message (so that the user knows why the process died),
  88.  * and gives the process an untrappable SIGSEGV.
  89.  */
  90. void oom(struct task_struct * task)
  91. {
  92.     printk("\nout of memory\n");
  93.     task->sigaction[SIGKILL-1].sa_handler = NULL;
  94.     task->blocked &= ~(1<<(SIGKILL-1));
  95.     send_sig(SIGKILL,task,1);
  96. }
  97.  
  98. static inline void nocache_page (unsigned long vaddr)
  99. {
  100.     if (boot_info.cputype & CPU_68040) {
  101.         /* address of kernel root table */
  102.         extern unsigned long *krt;
  103.         unsigned long desc, *kptr, *ktbl;
  104.  
  105.         desc = krt[L1_INDEX(vaddr)];
  106.         kptr = (unsigned long *)PTOV(desc & TABLE_MASK);        /* MR */
  107.         desc = kptr[L2_INDEX(vaddr)];
  108.         ktbl = (unsigned long *)PTOV(desc & PAGE_MASK);
  109.         desc = ktbl[L3_INDEX(vaddr)];
  110.         ktbl[L3_INDEX(vaddr)] = (desc & CACHEMASK040) | PAGE_NOCACHE;
  111.     }
  112. }
  113.  
  114. static inline void cache_page (unsigned long vaddr)
  115. {
  116.     if (boot_info.cputype & CPU_68040) {
  117.         /* address of kernel root table */
  118.         extern unsigned long *krt;
  119.         unsigned long desc, *kptr, *ktbl;
  120.  
  121.         desc = krt[L1_INDEX(vaddr)];
  122.         kptr = (unsigned long *)PTOV(desc & TABLE_MASK);        /* MR */
  123.         desc = kptr[L2_INDEX(vaddr)];
  124.         ktbl = (unsigned long *)PTOV(desc & PAGE_MASK);
  125.         desc = ktbl[L3_INDEX(vaddr)];
  126.         ktbl[L3_INDEX(vaddr)] = (desc & CACHEMASK040) | PAGE_CACHE040;
  127.     }
  128. }
  129.  
  130. static void free_one_table(unsigned long *page_ptr)
  131. {
  132.     int j;
  133.     unsigned long pg_table = *page_ptr;
  134.     unsigned long *page_table, pg_table_v;
  135.  
  136.     if (!pg_table)
  137.         return;
  138.     *page_ptr = 0;
  139.     pg_table_v = PTOV(pg_table & PAGE_MASK);
  140.     if (pg_table_v >= high_memory || !(pg_table & PAGE_TABLE)) {
  141.         printk("Bad page table: [%p]=%08lx\n",page_ptr,pg_table);
  142.         return;
  143.     }
  144.     if (mem_map[MAP_NR(pg_table_v)] & MAP_PAGE_RESERVED)
  145.         return;
  146.     page_table = (unsigned long *) pg_table_v;
  147.  
  148.     for (j = 0 ; j < NUM_L3_ENTRIES ; j++, page_table++) {
  149.         unsigned long pg = *page_table;
  150.  
  151.         if (!pg)
  152.             continue;
  153.         *page_table = 0;
  154.         if (pg & PAGE_PRESENT)
  155.             free_page(PTOV(PAGE_MASK & pg));
  156.         else
  157.             swap_free(pg);
  158.     }
  159.     cache_page (pg_table_v);
  160.     free_page(pg_table_v);
  161. }
  162.  
  163. static void free_one_ptr_table(unsigned long *root)
  164. {
  165.     int j;
  166.     unsigned long ptr_desc = *root;
  167.     unsigned long *ptr_table, ptr_table_v;
  168.  
  169.     if (!ptr_desc)
  170.         return;
  171.     *root = 0;
  172.     ptr_table_v = PTOV(ptr_desc & TABLE_MASK);
  173.     if (ptr_table_v >= high_memory || !(ptr_desc & PAGE_TABLE)) {
  174.         printk("Bad pointer table: [%p]=%08lx\n",root,ptr_desc);
  175.         return;
  176.     }
  177.     ptr_table = (unsigned long *) ptr_table_v;
  178.  
  179.     for (j = 0 ; j < NUM_L2_ENTRIES ; j++, ptr_table += 16)
  180.         free_one_table (ptr_table);
  181.     free_pointer_table ((unsigned long *)ptr_table_v);
  182. }
  183.  
  184. /*
  185.  * This function clears all user-level page tables of a process - this
  186.  * is needed by execve(), so that old pages aren't in the way. Note that
  187.  * unlike 'free_page_tables()', this function still leaves a valid
  188.  * page-table-tree in memory: it just removes the user pages. The two
  189.  * functions are similar, but there is a fundamental difference.
  190.  */
  191. void clear_page_tables(struct task_struct * tsk)
  192. {
  193.     int i;
  194.     unsigned long *root;
  195.  
  196.     if (!tsk)
  197.         return;
  198.     if (tsk == task[0])
  199.         panic("task[0] (swapper) doesn't support exec()\n");
  200.  
  201.     root = tsk->tss.pagedir_v;
  202.     if (!root || root == task[0]->tss.pagedir_v) {
  203.         printk("Trying to clear kernel page-directory: not good\n");
  204.         return;
  205.     }
  206.  
  207.     /*
  208.      * setup the user-mode root table, in case we were using the
  209.      * swapper root table.
  210.      */
  211.     tsk->tss.crp[1] = tsk->tss.pagedir_p;
  212.     if (tsk == current)
  213.         if (boot_info.cputype & CPU_68040)
  214.             asm ("movel %0,d0\n\t"
  215.                  ".long 0x4e7b0806" /* movec d0,urp */
  216.                  : /* no outputs */
  217.                  : "g" (tsk->tss.crp[1])
  218.                  : "d0");
  219.         else
  220.             asm ("pmove %0@,crp"
  221.                  : /* no outputs */
  222.                  : "a" (tsk->tss.crp));
  223.  
  224.     /* XXX */
  225.     /*
  226.      * root tables haven't got a use count in the mem_map
  227.      * since there isn't a 1-1 relationship between pages
  228.      * and root tables.
  229.      *
  230.      * Because of this, we will use a bletcherous hack where
  231.      * the last entry in the root table (entry 127) contains
  232.      * the number of uses - 1.
  233.      * Thus a regular root table will have 0 in this entry.
  234.      * A cloned root table will have > 0 in this entry.
  235.      *
  236.      * Note that this entry is never used by a user process.
  237.      */
  238.     if (root[NUM_L1_ENTRIES-1] > 0) {
  239.         unsigned long *new_root;
  240.  
  241.         if (!(new_root = get_root_table ())) {
  242.             oom(tsk);
  243.             return;
  244.         }
  245.         for (i = 0; i < NUM_L1_ENTRIES-1; i++)
  246.             new_root[i] = root[i];
  247.         new_root[NUM_L1_ENTRIES-1] = 0;
  248.         root[NUM_L1_ENTRIES-1]--;
  249.         tsk->tss.pagedir_v = new_root;
  250.         tsk->tss.pagedir_p = VTOP(new_root);
  251.         return;
  252.     }
  253.     for (i = 0 ; i < NUM_L1_ENTRIES-1; i++, root++)
  254.         free_one_ptr_table(root);
  255.     invalidate();
  256.     return;
  257. }
  258.  
  259. /*
  260.  * This function frees up all page tables of a process when it exits.
  261.  */
  262. void free_page_tables(struct task_struct * tsk)
  263. {
  264.     int i;
  265.     unsigned long *root;
  266.  
  267.     if (!tsk)
  268.         return;
  269.     if (tsk == task[0]) {
  270.         printk("task[0] (swapper) killed: unable to recover\n");
  271.         panic("Trying to free up swapper memory space");
  272.     }
  273.     root = tsk->tss.pagedir_v;
  274.     if (!root || root == task[0]->tss.pagedir_v) {
  275.         printk("Trying to free kernel root table: not good\n");
  276.         return;
  277.     }
  278.     /* XXX */
  279.     /*
  280.      * root tables haven't got a use count in the mem_map
  281.      * since there isn't a 1-1 relationship between pages
  282.      * and root tables.
  283.      *
  284.      * Because of this, we will use a bletcherous hack where
  285.      * the last entry in the root table (entry 127) contains
  286.      * the number of uses - 1.
  287.      * Thus a regular root table will have 0 in this entry.
  288.      * A cloned root table will have > 0 in this entry.
  289.      *
  290.      * Note that this entry is never used by a user process.
  291.      */
  292.     if (root[NUM_L1_ENTRIES-1] > 0) {
  293.         root[NUM_L1_ENTRIES-1]--;
  294.         return;
  295.     }
  296.  
  297.     for (i = 0 ; i < NUM_L1_ENTRIES-1; i++, root++)
  298.         free_one_ptr_table(root);
  299.  
  300.     free_root_table (tsk->tss.pagedir_v);
  301.     tsk->tss.pagedir_v = 0;
  302.     tsk->tss.pagedir_p = 0;
  303.     invalidate();
  304. }
  305.  
  306. /*
  307.  * clone_page_tables() clones the page table for a process - both
  308.  * processes will have the exact same pages in memory. There are
  309.  * probably races in the memory management with cloning, but we'll
  310.  * see..
  311.  */
  312. int clone_page_tables(struct task_struct * tsk)
  313. {
  314.     unsigned long *root;
  315.  
  316.     root = current->tss.pagedir_v;
  317.  
  318.     /* XXX */
  319.     /*
  320.      * root tables haven't got a use count in the mem_map
  321.      * since there isn't a 1-1 relationship between pages
  322.      * and root tables.
  323.      *
  324.      * Because of this, we will use a bletcherous hack where
  325.      * the last entry in the root table (entry 127) contains
  326.      * the number of uses - 1.
  327.      * Thus a regular root table will have 0 in this entry.
  328.      * A cloned root table will have > 0 in this entry.
  329.      *
  330.      * Note that this entry is never used by a user process.
  331.      */
  332.     root[NUM_L1_ENTRIES-1]++;
  333.     tsk->tss.pagedir_v = root;
  334.     tsk->tss.pagedir_p = VTOP (root);
  335.     tsk->tss.crp[0] = 0x80000000 | PAGE_SHORT;
  336.     tsk->tss.crp[1] = tsk->tss.pagedir_p;
  337.     return 0;
  338. }
  339.  
  340. static int copy_pointer_table (struct task_struct *tsk,
  341.                    unsigned long old_ptr_desc,
  342.                    unsigned long *new_root)
  343. {
  344.     int i;
  345.     unsigned long *old_ptr_table, *new_ptr_table, new_ptr_vaddr;
  346.  
  347.     /* get a new pointer table, can't allocate, no fork */
  348.     if (!(new_ptr_table = get_pointer_table ())) {
  349.         free_page_tables(tsk);
  350.         return -ENOMEM;
  351.     }
  352.     new_ptr_vaddr = (unsigned long)new_ptr_table;
  353.  
  354.     /* process each page table in the pointer table */
  355.     old_ptr_table = (unsigned long *)PTOV(old_ptr_desc & TABLE_MASK);
  356.  
  357.     for (i = 0 ; i < NUM_L2_ENTRIES ;
  358.          i++,old_ptr_table += 16, new_ptr_table += 16) {
  359.         int j;
  360.         unsigned long old_pg_table, *old_page_tablep;
  361.         unsigned long *new_page_tablep;
  362.  
  363.         /* read descriptor for page table */
  364.         old_pg_table = *old_ptr_table;
  365.  
  366.         /* if it is an invalid descriptor, continue to the next */
  367.         if (!old_pg_table)
  368.             continue;
  369.  
  370.         /*
  371.          * if the address is too high, or it is not a short
  372.          * descriptor, things are screwy.
  373.          */
  374.         if (PTOV(old_pg_table) >= high_memory
  375.             || !(old_pg_table & PAGE_TABLE)) {
  376.             printk("copy_page_tables: bad page table: "
  377.                    "probable memory corruption\n");
  378.             *old_ptr_table = 0;
  379.             continue;
  380.         }
  381.  
  382.         /*
  383.          * if it is a reserved entry (won't be, with the separate kernel
  384.          * and user virtual address spaces for the M68K port.
  385.          */
  386.         if (mem_map[MAP_NR(PTOV(old_pg_table))] & MAP_PAGE_RESERVED) {
  387.             *new_ptr_table = old_pg_table;
  388.             continue;
  389.         }
  390.  
  391.         /* setup a page table */
  392.         if (!(new_page_tablep = get_page_table (new_ptr_table)))
  393.             return -ENOMEM;
  394.  
  395.         /* process each page in the page table */
  396.         old_page_tablep = (unsigned long *)PTOV(old_pg_table & PAGE_MASK);
  397.         for (j = 0 ; j < PTRS_PER_PAGE ; j++,old_page_tablep++,new_page_tablep++) {
  398.             unsigned long pg;
  399.  
  400.             /* read page descriptor from table */
  401.             pg = *old_page_tablep;
  402.  
  403.             /* if invalid page, continue */
  404.             if (!pg)
  405.                 continue;
  406.  
  407.             /* check for swapped out page (invalid desc, nonzero) */
  408.             if (!(pg & PAGE_PRESENT)) {
  409.                 *new_page_tablep = swap_duplicate(pg);
  410.                 continue;
  411.             }
  412.  
  413.             /*
  414.              * share the page in both process maps, but write protect
  415.              * it in both, to catch attempts to write (copy on write)
  416.              */
  417.             if ((pg & (PAGE_RONLY | PAGE_COW)) == PAGE_COW)
  418.                 pg |= PAGE_RONLY;
  419.             *new_page_tablep = pg;
  420.             if (mem_map[MAP_NR(PTOV(pg & PAGE_MASK))] & MAP_PAGE_RESERVED)
  421.                 continue;
  422.             *old_page_tablep = pg;
  423.             mem_map[MAP_NR(PTOV(pg & PAGE_MASK))]++;
  424.         }
  425.     }
  426.  
  427.     /* write the new pointer table descriptor to the new root table */
  428.     *new_root = VTOP(new_ptr_vaddr) | PAGE_TABLE;
  429.  
  430.     return 0;
  431. }
  432.  
  433. /*
  434.  * copy_page_tables() just copies the whole process memory range:
  435.  * note the special handling of RESERVED (ie kernel) pages, which
  436.  * means that they are always shared by all processes.
  437.  */
  438. int copy_page_tables(struct task_struct * tsk)
  439. {
  440.     int i;
  441.     unsigned long *old_root;
  442.     unsigned long *new_root;
  443.  
  444.     if (!(new_root = get_root_table ()))
  445.         return -ENOMEM;
  446.     old_root = current->tss.pagedir_v;
  447.     tsk->tss.pagedir_v = new_root;
  448.     tsk->tss.pagedir_p = VTOP(new_root);
  449.     tsk->tss.crp[0] = 0x80000000 | PAGE_SHORT;
  450.     tsk->tss.crp[1] = tsk->tss.pagedir_p;
  451.  
  452.     /*
  453.      * if old root table is the same as in the swapper task (task 0),
  454.      * just re-use the swapper root table.
  455.      */
  456.     if (current->tss.crp[1] == task[0]->tss.crp[1]) {
  457.         tsk->tss.crp[1] = task[0]->tss.pagedir_p;
  458.         goto end;
  459.     }
  460.  
  461.     for (i = 0 ; i < NUM_L1_ENTRIES-1 ; i++, old_root++,new_root++) {
  462.         unsigned long old_ptr_desc;
  463.  
  464.         /* read descriptor for pointer table */
  465.         old_ptr_desc = *old_root;
  466.  
  467.         /* if it an invalid descriptor, continue to the next */
  468.         if (!old_ptr_desc)
  469.             continue;
  470.  
  471.         /*
  472.          * if the address is too high, or it is not a short
  473.          * descriptor, things are screwy.
  474.          */
  475.         if (PTOV(old_ptr_desc) >= high_memory
  476.             || !(old_ptr_desc & PAGE_TABLE)) {
  477.             printk("copy_page_tables: bad pointer table: "
  478.                    "probable memory corruption\n");
  479.             *old_root = 0;
  480.             continue;
  481.         }
  482.  
  483.         if (copy_pointer_table (tsk, old_ptr_desc, new_root))
  484.             return -ENOMEM;
  485.     }
  486. end:
  487.     invalidate();
  488.     return 0;
  489. }
  490.  
  491. /*
  492.  * a more complete version of free_page_tables which performs with page
  493.  * granularity.
  494.  */
  495. int unmap_page_range(unsigned long from, unsigned long size)
  496. {
  497.     unsigned long address;
  498.  
  499.     if (from & ~PAGE_MASK) {
  500.         printk ("unmap_page_range called with wrong alignment\n");
  501.         return -EINVAL;
  502.     }
  503.     size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
  504.  
  505.     for (address = from; size--; address += PAGE_SIZE) {
  506.         unsigned long *page_table, *rootp, *ptrp, page;
  507.  
  508.         rootp = ¤t->tss.pagedir_v[L1_INDEX(address)];
  509.         if (!(PAGE_TABLE & *rootp))
  510.             continue;
  511.         ptrp = (unsigned long *)PTOV(*rootp & TABLE_MASK);
  512.         ptrp += L2_INDEX(address);
  513.         if (!(PAGE_TABLE & *ptrp))
  514.             continue;
  515.         page_table = (unsigned long *)PTOV(*ptrp & PAGE_MASK);
  516.         page_table += L3_INDEX(address);
  517.         if ((page = *page_table) != 0) {
  518.             *page_table = 0;
  519.             if (page & PAGE_PRESENT) {
  520.                 if (!(mem_map[MAP_NR(PTOV(page & PAGE_MASK))]
  521.                       & MAP_PAGE_RESERVED))
  522.                     --current->rss;
  523.                 free_page(PTOV(page & PAGE_MASK));
  524.             } else
  525.                 swap_free(page);
  526.         }
  527.     }
  528.     invalidate();
  529.     return 0;
  530. }
  531.  
  532. int zeromap_page_range(unsigned long from, unsigned long size, int mask)
  533. {
  534.     unsigned long address;
  535.  
  536.     if (mask) {
  537.         if ((mask & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT) {
  538.             printk("zeromap_page_range: mask = %08x\n",mask);
  539.             return -EINVAL;
  540.         }
  541.         mask |= VTOP(ZERO_PAGE);
  542.     }
  543.  
  544.     if (from & ~PAGE_MASK) {
  545.         printk("zeromap_page_range: from = %08lx\n",from);
  546.         return -EINVAL;
  547.     }
  548.     size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
  549.  
  550.     for (address = from; size--; address += PAGE_SIZE) {
  551.         unsigned long *page_table, *rootp, *ptrp, page;
  552.  
  553.         rootp = ¤t->tss.pagedir_v[L1_INDEX(address)];
  554.         if (!(PAGE_TABLE & *rootp)) {
  555.             ptrp = get_pointer_table ();
  556.             if (PAGE_TABLE & *rootp)
  557.                 free_pointer_table (ptrp);
  558.             else if (!ptrp)
  559.                 return -ENOMEM;
  560.             *rootp = VTOP(ptrp) | PAGE_TABLE;
  561.         }
  562.         ptrp = (unsigned long *)PTOV(*rootp & TABLE_MASK);
  563.         ptrp += L2_INDEX(address);
  564.         if (!(PAGE_TABLE & *ptrp)) {
  565.             page_table = get_page_table (ptrp);
  566.             if (!page_table)
  567.                 return -ENOMEM;
  568.         } else
  569.             page_table = (unsigned long *)PTOV(*ptrp & PAGE_MASK);
  570.         page_table += L3_INDEX(address);
  571.         if ((page = *page_table) != 0) {
  572.             *page_table = 0;
  573.             if (page & PAGE_PRESENT) {
  574.                 if (!(mem_map[MAP_NR(PTOV(page & PAGE_MASK))]
  575.                       & MAP_PAGE_RESERVED))
  576.                     --current->rss;
  577.                 free_page(PTOV(page & PAGE_MASK));
  578.             } else
  579.                 swap_free(page);
  580.         }
  581.         *page_table = mask;
  582.     }
  583.     invalidate();
  584.     return 0;
  585. }
  586.  
  587. /*
  588.  * maps a range of physical memory into the requested pages. the old
  589.  * mappings are removed. any references to nonexistent pages results
  590.  * in null mappings (currently treated as "copy-on-access")
  591.  */
  592. int remap_page_range(unsigned long from, unsigned long to, unsigned long size, int mask)
  593. {
  594.     unsigned long address;
  595.  
  596.     if (mask) {
  597.         if ((mask & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT) {
  598.             printk("remap_page_range: mask = %08x\n",mask);
  599.             return -EINVAL;
  600.         }
  601.     }
  602.     if ((from & ~PAGE_MASK) || (to & ~PAGE_MASK)) {
  603.         printk("remap_page_range: from = %08lx, to=%08lx\n",from,to);
  604.         return -EINVAL;
  605.     }
  606.  
  607.     /* add cache bits if 68040 */
  608.     if (boot_info.cputype & CPU_68040)
  609.         mask = (mask & CACHEMASK040) | PAGE_CACHE040;
  610.  
  611.     size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
  612.  
  613.     for (address = from; size--; address += PAGE_SIZE) {
  614.         unsigned long *page_table, *rootp, *ptrp, page;
  615.  
  616.         rootp = ¤t->tss.pagedir_v[L1_INDEX(address)];
  617.         if (!(PAGE_TABLE & *rootp)) {
  618.             ptrp = get_pointer_table ();
  619.             if (PAGE_TABLE & *rootp)
  620.                 free_pointer_table (ptrp);
  621.             else if (!ptrp)
  622.                 return -ENOMEM;
  623.             *rootp = VTOP(ptrp) | PAGE_TABLE;
  624.         }
  625.         ptrp = (unsigned long *)PTOV(*rootp & TABLE_MASK);
  626.         ptrp += L2_INDEX(address);
  627.         if (!(PAGE_TABLE & *ptrp)) {
  628.             page_table = get_page_table (ptrp);
  629.             if (!page_table)
  630.                 return -ENOMEM;
  631.         } else
  632.             page_table = (unsigned long *)PTOV(*ptrp & PAGE_MASK);
  633.         page_table += L3_INDEX(address);
  634.         if ((page = *page_table) != 0) {
  635.             *page_table = 0;
  636.             if (page & PAGE_PRESENT) {
  637.                 if (!(mem_map[MAP_NR(PTOV(page & PAGE_MASK))]
  638.                       & MAP_PAGE_RESERVED))
  639.                     --current->rss;
  640.                 free_page(PTOV(page & PAGE_MASK));
  641.             } else
  642.                 swap_free(page);
  643.         }
  644.  
  645.         /*
  646.          * the first condition should return an invalid access
  647.          * when the page is referenced. current assumptions
  648.          * cause it to be treated as demand allocation in some
  649.          * cases.
  650.          */
  651.         if (!mask)
  652.             *page_table = 0;    /* not present */
  653.         else if (to >= high_memory)
  654.             *page_table = (VTOP(to) | mask);
  655.         else if (!mem_map[MAP_NR(to)])
  656.             *page_table = 0;    /* not present */
  657.         else {
  658.             *page_table = VTOP(to) | mask;
  659.             if (!(mem_map[MAP_NR(to)] & MAP_PAGE_RESERVED)) {
  660.                 ++current->rss;
  661.                 mem_map[MAP_NR(to)]++;
  662.             }
  663.         }
  664.         to += PAGE_SIZE;
  665.     }
  666.     invalidate();
  667.     return 0;
  668. }
  669.  
  670. /*
  671.  * This function puts a page in memory at the wanted address.
  672.  * It returns the physical address of the page gotten, 0 if
  673.  * out of memory (either when trying to access page-table or
  674.  * page.)
  675.  */
  676. unsigned long put_page(struct task_struct * tsk,unsigned long page,
  677.                unsigned long address, int prot)
  678. {
  679.     unsigned long *ptr_table, *page_table;
  680. /*
  681. printk("put_page pid %i page %lx addr %lx prot %lx\n", tsk->pid, page, address, prot);
  682. */
  683.     if ((prot & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT)
  684.         printk("put_page: prot = %08x\n",prot);
  685.  
  686.     if (page >= high_memory) {
  687.         printk("put_page: trying to put page %08lx at %08lx\n",page,
  688.            address);
  689.         return 0;
  690.     }
  691.  
  692.     ptr_table = &tsk->tss.pagedir_v[L1_INDEX(address)];
  693.     if (*ptr_table & PAGE_TABLE)
  694.         ptr_table = (unsigned long *) PTOV(*ptr_table & TABLE_MASK);
  695.     else {
  696.         printk("put_page: bad page root table entry\n");
  697.         oom(tsk);
  698.         return 0;
  699.     }
  700.     page_table = &ptr_table[L2_INDEX(address)];
  701.     if (*page_table & PAGE_TABLE)
  702.     page_table = (unsigned long *) PTOV(*page_table & PAGE_MASK);
  703.     else {
  704.     printk("put_page: bad pointer table entry\n");
  705.     oom(tsk);
  706.     *ptr_table = BAD_PAGETABLE | PAGE_TABLE;
  707.     return 0;
  708.     }
  709.     /* An 'invalidate' has to be done anyway, and as it affects only
  710.      * user entries, it can be done here. (MA)
  711.      */
  712.     invalidate();
  713.     page_table += L3_INDEX(address);
  714.     if (*page_table) {
  715.     printk("put_page: page already exists\n");
  716.     *page_table = 0;
  717.     }
  718.     *page_table = VTOP(page) | prot;
  719.  
  720.     return page;
  721. }
  722.  
  723. /*
  724.  * The previous function doesn't work very well if you also want to mark
  725.  * the page dirty: exec.c wants this, as it has earlier changed the page,
  726.  * and we want the dirty-status to be correct (for VM). Thus the same
  727.  * routine, but this time we mark it dirty too.
  728.  */
  729. unsigned long put_dirty_page (struct task_struct * tsk,
  730.                   unsigned long page,
  731.                   unsigned long address)
  732. {
  733.     unsigned long *page_table, *ptr_table, *rootp;
  734.  
  735.     if (page >= high_memory)
  736.         printk("put_dirty_page: trying to put page %08lx at %08lx\n",
  737.                page,address);
  738.     if (mem_map[MAP_NR(page)] != 1)
  739.         printk("mem_map disagrees with %08lx at %08lx\n",page,address);
  740.     rootp = &tsk->tss.pagedir_v[L1_INDEX(address)];
  741.     if (*rootp & PAGE_TABLE)
  742.         ptr_table = (unsigned long *)PTOV(*rootp & TABLE_MASK);
  743.     else if (!*rootp) {
  744.         ptr_table = get_pointer_table ();
  745.         *rootp = VTOP(ptr_table) | PAGE_TABLE;
  746.     } else {
  747.         printk("put_dirty_page: bad root table entry\n");
  748.         return 0;
  749.     }
  750.     ptr_table += L2_INDEX(address);
  751.     if (*ptr_table & PAGE_TABLE)
  752.         page_table = (unsigned long *)PTOV(*ptr_table & PAGE_MASK);
  753.     else if (!*ptr_table) {
  754.         if (!(page_table = get_page_table (ptr_table)))
  755.             return 0;
  756.     } else {
  757.         printk("put_dirty_page: bad pointer table entry\n");
  758.         return 0;
  759.     }
  760.     /* An 'invalidate' has to be done anyway, and as it affects only
  761.      * user entries, it can be done here. (MA)
  762.      */
  763.     invalidate();
  764.     page_table += L3_INDEX(address);
  765.     if (*page_table) {
  766.         printk("put_dirty_page: page already exists\n");
  767.         *page_table = 0;
  768.     }
  769.     *page_table = VTOP(page) | (PAGE_DIRTY | PAGE_PRIVATE | PAGE_ACCESSED);  /* MA */
  770.     return page;
  771. }
  772.  
  773. /*
  774.  * This routine handles present pages, when users try to write
  775.  * to a shared page. It is done by copying the page to a new address
  776.  * and decrementing the shared-page counter for the old page.
  777.  *
  778.  * Note that we do many checks twice (look at do_wp_page()), as
  779.  * we have to be careful about race-conditions.
  780.  *
  781.  * Goto-purists beware: the only reason for goto's here is that it results
  782.  * in better assembly code.. The "default" path will see no jumps at all.
  783.  */
  784. static void __do_wp_page(unsigned long writecycle, unsigned long address,
  785.              struct task_struct * tsk, unsigned long user_usp)
  786. {
  787.     unsigned long *rootp, desc, *ptep, *ptrp, old_page, prot;
  788.     unsigned long new_page;
  789.  
  790. #ifdef DEBUG
  791.     if (user_usp)
  792.         printk ("write protected page fault at addr %#lx in task %p\n",
  793.             address, tsk);
  794. #endif
  795.     new_page = __get_free_page(GFP_KERNEL);
  796.     rootp = &tsk->tss.pagedir_v[L1_INDEX(address)];
  797.     desc = *rootp;
  798.     if ((desc & PAGE_TABLE) != PAGE_TABLE
  799.         || PTOV (desc & TABLE_MASK) >= high_memory)
  800.         panic ("do_wp_page: invalid root table entry %08lx", desc);
  801.     ptrp = (unsigned long *)PTOV(desc & TABLE_MASK);
  802.     ptrp += L2_INDEX(address);
  803.     desc = *ptrp;
  804.     if ((desc & PAGE_TABLE) != PAGE_TABLE
  805.         || PTOV (desc & TABLE_MASK) >= high_memory)
  806.         goto bad_wp_pagetable;
  807.     ptep = (unsigned long *)PTOV(desc & PAGE_MASK) + L3_INDEX(address);
  808.     old_page = *ptep;
  809.     if (!(old_page & PAGE_PRESENT))
  810.         goto end_wp_page;
  811.     if (PTOV (old_page) >= high_memory)
  812.         goto bad_wp_page;
  813.     if (!(old_page & PAGE_RONLY))
  814.         goto end_wp_page;
  815.     /* minor page fault (copy on write) */
  816.     tsk->min_flt++;
  817.     prot = ((old_page & ~PAGE_MASK) & ~PAGE_RONLY) | PAGE_RW;
  818.     old_page = PTOV(old_page & PAGE_MASK);
  819.     /* if page was used only once (no longer shared) enable read write */
  820.     if (mem_map[MAP_NR(old_page)] != 1) {
  821.         if (new_page) {
  822.             if (mem_map[MAP_NR(old_page)] & MAP_PAGE_RESERVED)
  823.                 ++tsk->rss;
  824.             copy_page (old_page, new_page);
  825.             *ptep = VTOP (new_page) | prot;
  826.             free_page (old_page);
  827.             invalidate();
  828.             return;
  829.         }
  830.         free_page(old_page);
  831.         oom (tsk);
  832.         *ptep = VTOP (BAD_PAGE) | prot;
  833.         invalidate();
  834.         return;
  835.     }
  836.     *ptep &= ~PAGE_RONLY;
  837.     invalidate ();
  838.     if (new_page)
  839.         free_page (new_page);
  840.     return;
  841. bad_wp_page:
  842.     printk ("do_wp_page: bogus page at address %08lx (%08lx)\n", address,
  843.         old_page);
  844.     *ptep = VTOP (BAD_PAGE) | PAGE_SHARED;
  845.     send_sig (SIGKILL, tsk, 1);
  846.     goto end_wp_page;
  847. bad_wp_pagetable:
  848.     printk ("do_wp_page: bogus page-table at address %08lx (%08lx)\n",
  849.         address, desc);
  850.     *ptrp = VTOP (BAD_PAGETABLE) | PAGE_TABLE;
  851.     send_sig (SIGKILL, tsk, 1);
  852. end_wp_page:
  853.     if (new_page)
  854.         free_page (new_page);
  855.     return;
  856. }
  857. /*
  858. printk("do_wp_page: error %lx address %lx pid %i stack %lx\n", error_code, address, tsk->pid, user_usp);
  859. */
  860. /*
  861.  * check that a page table change is actually needed, and call
  862.  * the low-level function only in that case..
  863.  */
  864. void do_wp_page(unsigned long error_code, unsigned long address,
  865.         struct task_struct * tsk, unsigned long user_usp)
  866. {
  867.     unsigned long page, ptrdesc;
  868.     unsigned long *rootp, *pg_table;
  869.  
  870. #ifdef DEBUG
  871.     printk ("do_wp_page (address=%#lx, user_usp=%#lx\n", address,
  872.         user_usp);
  873. #endif
  874.     rootp = &tsk->tss.pagedir_v[L1_INDEX(address)];
  875.     ptrdesc = *rootp;
  876.     if (!ptrdesc) {
  877.         /*
  878.          * this code just had "return", but that doesn't seem correct;
  879.          * it will just trap again.  I added a printk
  880.          * Hamish Macdonald Tue May 18 17:29:00 1993
  881.          * Changed printk/return to panic.
  882.          * Hamish Macdonald Friday 19-Nov-93 22:46:45
  883.          */
  884.         panic ("do_wp_page: unexplained null root table entry for %lx",
  885.                address);
  886.     }
  887.     pg_table = (unsigned long *)PTOV(ptrdesc & TABLE_MASK);
  888.     pg_table += L2_INDEX(address);
  889.     page = *pg_table;
  890.     if (!page) {
  891.         /*
  892.          * this code just had "return", but that doesn't seem correct;
  893.          * it will just trap again.  I added a printk
  894.          * Hamish Macdonald Tue May 18 17:29:00 1993
  895.          * Changed printk/return to panic.
  896.          * Hamish Macdonald Friday 19-Nov-93 22:46:45
  897.          */
  898.         panic ("do_wp_page: unexplained null pointer table entry for %lx", address);
  899.     }
  900.     if ((page & PAGE_TABLE) && PTOV(page) < high_memory) {
  901.         pg_table = (unsigned long *)PTOV(page & PAGE_MASK);
  902.         pg_table += L3_INDEX(address);
  903.         page = *pg_table;
  904.         if (!(page & PAGE_PRESENT))
  905.             return;
  906.         if (!(page & PAGE_RONLY))
  907.             return;
  908.         if (!(page & PAGE_COW)) {
  909.             if (user_usp && tsk == current) {
  910. #if 1 /*def DEBUG*/
  911.                 if (boot_info.cputype & CPU_68040)
  912.                     printk("do_wp_page: (oh no! probably once again '040 ATC inconsistency)\n");
  913. #endif
  914.                 send_sig(SIGSEGV, tsk, 1);
  915.                 return;
  916.  
  917.             }
  918.         }
  919.         if (mem_map[MAP_NR(PTOV(page))] == 1) {
  920.             *pg_table = (*pg_table & ~PAGE_RONLY) | PAGE_DIRTY;
  921. #ifdef DEBUG
  922.             printk ("do_wp_page: just marking page RW\n");
  923. #endif
  924.             invalidate();
  925.             return;
  926.         }
  927.         __do_wp_page(error_code, address, tsk, user_usp);
  928.         return;
  929.     }
  930.     printk("bad page table entry %08lx\n",page);
  931.     *pg_table = 0;
  932. }
  933.  
  934. int verify_area(int type, void * addr, unsigned long size)
  935. {
  936.     unsigned long start;
  937.  
  938.     start = (unsigned long) addr;
  939. #ifdef __i386__
  940.     if (start >= TASK_SIZE)
  941.         return -EFAULT;
  942.     if (size > TASK_SIZE - start)
  943.         return -EFAULT;
  944. #endif
  945.     if (wp_works_ok || type == VERIFY_READ || !size)
  946.         return 0;
  947.     if (!size)
  948.         return 0;
  949.     size--;
  950.     size += start & ~PAGE_MASK;
  951.     size >>= PAGE_SHIFT;
  952.     start &= PAGE_MASK;
  953.     do {
  954.         do_wp_page(1,start,current,0);
  955.         start += PAGE_SIZE;
  956.     } while (size--);
  957.     return 0;
  958. }
  959.  
  960. static inline void get_empty_page(struct task_struct * tsk, unsigned long address)
  961. {
  962.     unsigned long tmp;
  963.  
  964.     if (!(tmp = get_free_page(GFP_KERNEL))) {
  965.     oom(tsk);
  966.     tmp = BAD_PAGE;
  967.     }
  968.     if (!put_page(tsk,tmp,address,PAGE_PRIVATE))
  969.     free_page(tmp);
  970. }
  971.  
  972. /*
  973.  * try_to_share() checks the page at address "address" in the task "p",
  974.  * to see if it exists, and if it is clean. If so, share it with the current
  975.  * task.
  976.  *
  977.  * NOTE! This assumes we have checked that p != current, and that they
  978.  * share the same executable or library.
  979.  *
  980.  * We may want to fix this to allow page sharing for PIC pages at different
  981.  * addresses so that ELF will really perform properly. As long as the vast
  982.  * majority of sharable libraries load at fixed addresses this is not a
  983.  * big concern. Any sharing of pages between the buffer cache and the
  984.  * code space reduces the need for this as well.  - ERY
  985.  */
  986. static int try_to_share(unsigned long address, struct task_struct * tsk,
  987.             struct task_struct * p, unsigned long writecycle,
  988.             unsigned long newpage)
  989. {
  990.     unsigned long from, to, desc;
  991.     unsigned long *rootp_f, *from_ptr, *from_page;
  992.     unsigned long *rootp_t, *to_ptr, *to_page;
  993.     unsigned long virt_addr;
  994.  
  995.     rootp_f = &p->tss.pagedir_v[L1_INDEX(address)];
  996.     rootp_t = &tsk->tss.pagedir_v[L1_INDEX(address)];
  997.  
  998.     /* is there a pointer table in from? */
  999.     desc = *rootp_f;
  1000.     if (!(desc & PAGE_TABLE))
  1001.     return 0;
  1002.     from_ptr = (unsigned long *)PTOV(desc & TABLE_MASK) + L2_INDEX(address);
  1003.     /* is there a page table in from? */
  1004.     desc = *from_ptr;
  1005.     if (!(desc & PAGE_TABLE))
  1006.     return 0;
  1007.     from_page = (unsigned long *)PTOV(desc & PAGE_MASK) + L3_INDEX(address);
  1008.     from = *from_page;
  1009.     /* is the page clean and present? */
  1010.     if ((from & (PAGE_PRESENT | PAGE_DIRTY)) != PAGE_PRESENT)
  1011.     return 0;
  1012.     virt_addr = PTOV(from & PAGE_MASK);
  1013.     if (virt_addr >= high_memory)
  1014.     return 0;
  1015.     if (mem_map[MAP_NR(virt_addr)] & MAP_PAGE_RESERVED)
  1016.     return 0;
  1017.     /* is the destination ok? */
  1018.     desc = *rootp_t;
  1019.     if (!(desc & PAGE_TABLE))
  1020.     return 0;
  1021.     to_ptr = (unsigned long *)PTOV(desc & TABLE_MASK) + L2_INDEX(address);
  1022.     desc = *to_ptr;
  1023.     if (!(desc & PAGE_TABLE))
  1024.     return 0;
  1025.     to_page = (unsigned long *)PTOV(desc & PAGE_MASK) + L3_INDEX(address);
  1026.     if (*to_page)
  1027.     return 0;
  1028.     /* share them if read - do COW immediately otherwise */
  1029.     if (writecycle) {
  1030.         if (!newpage) /* did the page exist?  SRB. */
  1031.             return 0;
  1032.         copy_page(virt_addr,newpage);
  1033.         to = VTOP(newpage) | PAGE_PRIVATE;
  1034.     } else {
  1035.         mem_map[MAP_NR(virt_addr)]++;
  1036.         from |= PAGE_RONLY;
  1037.         to = from;
  1038.         if (newpage)
  1039.             free_page(newpage);
  1040.     }
  1041.     *from_page = from;
  1042.     *to_page = to;
  1043.     invalidate();
  1044.     return 1;
  1045. }
  1046.  
  1047. /*
  1048.  * share_page() tries to find a process that could share a page with
  1049.  * the current one. Address is the address of the wanted page relative
  1050.  * to the current data space.
  1051.  *
  1052.  * We first check if it is at all feasible by checking executable->i_count.
  1053.  * It should be >1 if there are other tasks sharing this inode.
  1054.  */
  1055.  int share_page(struct vm_area_struct * area, struct task_struct * tsk,
  1056.         struct inode * inode,
  1057.         unsigned long address, unsigned long error_code, unsigned long newpage)
  1058. {
  1059.     struct task_struct ** p;
  1060.  
  1061.     if (!inode || inode->i_count < 2 || !area->vm_ops)
  1062.         return 0;
  1063.     for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
  1064.         if (!*p)
  1065.             continue;
  1066.         if (tsk == *p)
  1067.             continue;
  1068.         if (inode != (*p)->executable) {
  1069.               if(!area) continue;
  1070.             /* Now see if there is something in the VMM that
  1071.                we can share pages with */
  1072.             if(area){
  1073.               struct vm_area_struct * mpnt;
  1074.               for (mpnt = (*p)->mmap; mpnt; mpnt = mpnt->vm_next) {
  1075.                 if (mpnt->vm_ops == area->vm_ops &&
  1076.                    mpnt->vm_inode->i_ino == area->vm_inode->i_ino&&
  1077.                    mpnt->vm_inode->i_dev == area->vm_inode->i_dev){
  1078.                   if (mpnt->vm_ops->share(mpnt, area, address))
  1079.                 break;
  1080.                 };
  1081.               };
  1082.               if (!mpnt) continue;  /* Nope.  Nuthin here */
  1083.             };
  1084.         }
  1085.         if (try_to_share(address,tsk,*p,error_code,newpage))
  1086.             return 1;
  1087.     }
  1088.     return 0;
  1089. }
  1090.  
  1091. static struct ptable_desc {
  1092.     struct ptable_desc *prev;
  1093.     struct ptable_desc *next;
  1094.     unsigned long       page;
  1095.     unsigned char       alloced;
  1096. } ptable_list = { &ptable_list, &ptable_list, 0, 0xff };
  1097.  
  1098. #define PD_NONEFREE(dp) ((dp)->alloced == 0xff)
  1099. #define PD_ALLFREE(dp) ((dp)->alloced == 0)
  1100. #define PD_TABLEFREE(dp,i) (!((dp)->alloced & (1<<(i))))
  1101. #define PD_MARKUSED(dp,i) ((dp)->alloced |= (1<<(i)))
  1102. #define PD_MARKFREE(dp,i) ((dp)->alloced &= ~(1<<(i)))
  1103.  
  1104. unsigned long *get_pointer_table (void)
  1105. {
  1106.     unsigned long table = 0, flags;
  1107.     struct ptable_desc *dp = ptable_list.next;
  1108.     int i;
  1109.  
  1110.     /*
  1111.      * For a pointer table for a user process address space, a
  1112.      * table is taken from a page allocated for the purpose.  Each
  1113.      * page can hold 8 pointer tables.  The page is remapped in
  1114.      * virtual address space to be noncacheable.
  1115.      */
  1116.     if (PD_NONEFREE (dp)) {
  1117.  
  1118.         if (!(dp = kmalloc (sizeof(struct ptable_desc),GFP_KERNEL))) {
  1119.             return 0;
  1120.         }
  1121.  
  1122.         if (!(dp->page = __get_free_page (GFP_KERNEL))) {
  1123.             kfree (dp);
  1124.             return 0;
  1125.         }
  1126.  
  1127.         nocache_page (dp->page);
  1128.  
  1129.         dp->alloced = 0;
  1130.         /* put at head of list */
  1131.         save_flags (flags);
  1132.         cli ();
  1133.         dp->next = ptable_list.next;
  1134.         dp->prev = ptable_list.next->prev;
  1135.         ptable_list.next->prev = dp;
  1136.         ptable_list.next = dp;
  1137.         restore_flags (flags);
  1138.     }
  1139.  
  1140.     for (i = 0; i < 8; i++)
  1141.         if (PD_TABLEFREE (dp, i)) {
  1142.             PD_MARKUSED (dp, i);
  1143.             table = dp->page + PTABLE_SIZE*i;
  1144.             break;
  1145.         }
  1146.  
  1147.     if (PD_NONEFREE (dp)) {
  1148.         /* move to end of list */
  1149.         save_flags (flags);
  1150.         cli ();
  1151.         dp->prev->next = dp->next;
  1152.         dp->next->prev = dp->prev;
  1153.  
  1154.         dp->next = ptable_list.next->prev;
  1155.         dp->prev = ptable_list.prev;
  1156.         ptable_list.prev->next = dp;
  1157.         ptable_list.prev = dp;
  1158.         restore_flags (flags);
  1159.     }
  1160.  
  1161.     memset ((void *)table, 0, PTABLE_SIZE);
  1162.  
  1163.     return (unsigned long *)table;
  1164. }
  1165.  
  1166. void free_pointer_table (unsigned long *ptable)
  1167. {
  1168.     struct ptable_desc *dp;
  1169.     unsigned long page = (unsigned long)ptable & PAGE_MASK;
  1170.     int index = ((unsigned long)ptable - page)/PTABLE_SIZE;
  1171.     unsigned long flags;
  1172.  
  1173.     for (dp = ptable_list.next; dp->page && dp->page != page; dp = dp->next)
  1174.         ;
  1175.  
  1176.     if (!dp->page)
  1177.         panic ("unable to find desc for ptable %p on list!", ptable);
  1178.  
  1179.     if (PD_TABLEFREE (dp, index))
  1180.         panic ("table already free!");
  1181.  
  1182.     PD_MARKFREE (dp, index);
  1183.  
  1184.     if (PD_ALLFREE (dp)) {
  1185.         /* all tables in page are free, free page */
  1186.         save_flags (flags);
  1187.         cli ();
  1188.         dp->prev->next = dp->next;
  1189.         dp->next->prev = dp->prev;
  1190.         restore_flags (flags);
  1191.         cache_page (dp->page);
  1192.         free_page (dp->page);
  1193.         kfree (dp);
  1194.         return;
  1195.     } else {
  1196.         /*
  1197.          * move this descriptor the the front of the list, since
  1198.          * it has one or more free tables.
  1199.          */
  1200.         save_flags (flags);
  1201.         cli ();
  1202.         dp->prev->next = dp->next;
  1203.         dp->next->prev = dp->prev;
  1204.  
  1205.         dp->next = ptable_list.next;
  1206.         dp->prev = ptable_list.next->prev;
  1207.         ptable_list.next->prev = dp;
  1208.         ptable_list.next = dp;
  1209.         restore_flags (flags);
  1210.     }
  1211. }
  1212.  
  1213. unsigned long *get_page_table (unsigned long *ptr)
  1214. {
  1215.     unsigned long page, tmp;
  1216.     int i;
  1217.  
  1218.     page = get_free_page(GFP_KERNEL);
  1219.     if (PAGE_TABLE & *ptr) {
  1220.         free_page (page);
  1221.         return (unsigned long *)PTOV(*ptr & PAGE_MASK);
  1222.     }
  1223.     if (page) {
  1224.         nocache_page (page);
  1225.         tmp = VTOP(page) | PAGE_TABLE;
  1226.         for (i = 0; i < 16; i++, tmp += 256)
  1227.             ptr[i] = tmp;
  1228.         return (unsigned long *)page;
  1229.     }
  1230.  
  1231.     return NULL;
  1232. }
  1233.  
  1234. void free_page_table (unsigned long *ptr)
  1235. {
  1236.     unsigned long page;
  1237.     int i;
  1238.  
  1239.     if (!(PAGE_TABLE & *ptr))
  1240.         return;
  1241.     page = PTOV(*ptr & PAGE_MASK);
  1242.     for (i = 0; i < 16; i++)
  1243.         ptr[i] = 0;
  1244.     cache_page (page);
  1245.     free_page (page);
  1246. }
  1247.  
  1248. /*
  1249.  * fill in an empty page-table if none exists.
  1250.  */
  1251. static inline unsigned long get_empty_pgtable(struct task_struct *tsk,
  1252.                           unsigned long address)
  1253. {
  1254.     unsigned long tmp, *pagep;
  1255.     unsigned long *rptr, *ptr;
  1256.     int i;
  1257.  
  1258.     rptr = &tsk->tss.pagedir_v[L1_INDEX(address)];
  1259.     if (!*rptr) {
  1260.         /* no pointer table, allocate one */
  1261.         ptr = get_pointer_table ();
  1262.         if (*rptr & PAGE_TABLE)
  1263.             free_pointer_table (ptr);
  1264.         else
  1265.             *rptr = VTOP(ptr) | PAGE_TABLE;
  1266.     } else if (!(PAGE_TABLE & *rptr))
  1267.         panic("get_empty_pgtable: bad root table entry %#lx\n", *rptr);
  1268.  
  1269.     ptr = (unsigned long *)PTOV (*rptr & TABLE_MASK);
  1270.     ptr += L2_INDEX(address);
  1271.  
  1272.     if (PAGE_TABLE & *ptr)
  1273.         return *ptr;
  1274.     else if (*ptr) {
  1275.         printk("get_empty_pgtable: bad pointer table entry \n");
  1276.         *ptr = 0;
  1277.     }
  1278.  
  1279.     pagep = get_page_table (ptr);
  1280.     if (pagep)
  1281.         return *ptr;
  1282.     oom(current);
  1283.     tmp = VTOP(BAD_PAGETABLE) | PAGE_TABLE;
  1284.     for (i = 0; i < 16; i++, tmp += 256)
  1285.         ptr[i] = tmp;
  1286.     return 0;
  1287. }
  1288.  
  1289. void do_no_page(unsigned long error_code, unsigned long address,
  1290.         struct task_struct *tsk, unsigned long usp)
  1291. {
  1292.     unsigned long tmp;
  1293.     unsigned long page, *p;
  1294.     struct vm_area_struct * mpnt;
  1295. /*
  1296. printk("do_no_page: error %lx address %lx pid %i stack %lx\n", error_code, address, tsk->pid, usp);
  1297. */
  1298.     page = get_empty_pgtable(tsk,address);
  1299. #ifdef DEBUG
  1300.     if (!page)
  1301.         printk ("do_no_page: null return from get_empty_pgtable\n");
  1302.     else {
  1303.         printk ("do_no_page: empty_pgtable is %#lx\n", page);
  1304.         printk ("address=%#lx, usp=%#lx\n", address, usp);
  1305.         waitbut();
  1306.     }
  1307. #endif
  1308.     if (!page)
  1309.         return;
  1310.     p = (unsigned long *)PTOV(page & PAGE_MASK);
  1311.     tmp = p[L3_INDEX(address)];
  1312.     if (tmp & PAGE_PRESENT)
  1313.         return;
  1314.  
  1315.     /* resident set size has increased */
  1316.     ++tsk->rss;
  1317.  
  1318.     /* page was swapped out, flag major fault and swap it in */
  1319.     if (tmp) {
  1320.         ++tsk->maj_flt;
  1321.         swap_in(&p[L3_INDEX(address)]);
  1322.         return;
  1323.     }
  1324.     address &= PAGE_MASK;
  1325.     tmp = 0;
  1326.     for (mpnt = tsk->mmap ; mpnt != NULL; mpnt = mpnt->vm_next) {
  1327.         if (address < mpnt->vm_start)
  1328.             break;
  1329.         if (address >= mpnt->vm_end) {
  1330.             tmp = mpnt->vm_end;
  1331.             continue;
  1332.         }
  1333.         if (!mpnt->vm_ops || !mpnt->vm_ops->nopage) {
  1334.             ++tsk->min_flt;
  1335.             get_empty_page(tsk,address);
  1336.             return;
  1337.         }
  1338.         mpnt->vm_ops->nopage(error_code, mpnt, address);
  1339.         return;
  1340.     }
  1341.  
  1342.     ++tsk->min_flt;
  1343.     get_empty_page(tsk,address);
  1344.     if (tsk != current)
  1345.         return;
  1346.     if (address >= tsk->end_data && address < tsk->brk)
  1347.         return;
  1348.     if (mpnt && mpnt == tsk->stk_vma &&
  1349.         address - tmp > mpnt->vm_start - address &&
  1350.         tsk->rlim[RLIMIT_STACK].rlim_cur > mpnt->vm_end - address) {
  1351.         mpnt->vm_start = address;
  1352.         return;
  1353.     }
  1354.     send_sig(SIGSEGV,tsk,1);
  1355.     return;
  1356. }
  1357.  
  1358. /*
  1359.  * This routine handles page faults.  It determines the problem, and
  1360.  * then passes it off to one of the appropriate routines.
  1361.  */
  1362. asmlinkage void do_page_fault(struct frame *fp, unsigned long address,
  1363.                   unsigned long error_code)
  1364. {
  1365.     unsigned long user_esp = 0;
  1366.  
  1367.     if (!(fp->sr & PS_S))
  1368.         /* if !supervisor mode, set user stack pointer */
  1369.         user_esp = fp->usp;
  1370.  
  1371. #ifdef DEBUG
  1372.     printk ("fp->pc=%#lx, address=%#lx\n", fp->pc, address);
  1373. #endif
  1374.     if (address < TASK_SIZE) {
  1375.         if (error_code & 1)
  1376.             do_wp_page (error_code, address, current, user_esp);
  1377.         else
  1378.             do_no_page (error_code, address, current, user_esp);
  1379.         return;
  1380.     }
  1381.     printk("Unable to handle kernel paging request at address %08lx from %lx\n",address, fp->pc);
  1382.     die_if_kernel("Oops", fp, error_code);
  1383.     do_exit(SIGKILL);
  1384. }
  1385.  
  1386. /* This handles a generic mmap of a disk file */
  1387. void file_mmap_nopage(int error_code, struct vm_area_struct * area, unsigned long address)
  1388. {
  1389.     struct inode * inode = area->vm_inode;
  1390.     unsigned int block;
  1391.     unsigned long page;
  1392.     int nr[8];
  1393.     int i, j;
  1394.     int prot = area->vm_page_prot;
  1395.  
  1396.     address &= PAGE_MASK;
  1397.     block = address - area->vm_start + area->vm_offset;
  1398.     block >>= inode->i_sb->s_blocksize_bits;
  1399.  
  1400.     page = get_free_page(GFP_KERNEL);
  1401.     if (share_page(area, area->vm_task, inode, address, error_code, page)) {
  1402.         ++area->vm_task->min_flt;
  1403.         return;
  1404.     }
  1405.  
  1406.     ++area->vm_task->maj_flt;
  1407.     if (!page) {
  1408.         oom(current);
  1409.         put_page(area->vm_task, BAD_PAGE, address, PAGE_PRIVATE);
  1410.         return;
  1411.     }
  1412.     for (i=0, j=0; i< PAGE_SIZE ; j++, block++, i += inode->i_sb->s_blocksize)
  1413.         nr[j] = bmap(inode,block);
  1414.     if (error_code & 1)
  1415.         prot = (prot & ~PAGE_RONLY) | PAGE_DIRTY;
  1416.     page = bread_page(page, inode->i_dev, nr, inode->i_sb->s_blocksize, prot);
  1417.     cache_push (VTOP(page), PAGE_SIZE);          /* MA */
  1418.     if (prot & PAGE_RONLY) {
  1419.         if (share_page(area, area->vm_task, inode, address, error_code, page))
  1420.             return;
  1421.     }
  1422.     if (put_page(area->vm_task,page,address,prot))
  1423.         return;
  1424.     free_page(page);
  1425.     oom(current);
  1426. }
  1427.  
  1428. void file_mmap_free(struct vm_area_struct * area)
  1429. {
  1430.     if (area->vm_inode)
  1431.         iput(area->vm_inode);
  1432. #if 0
  1433.     if (area->vm_inode)
  1434.         printk("Free inode %x:%d (%d)\n",area->vm_inode->i_dev,
  1435.                  area->vm_inode->i_ino, area->vm_inode->i_count);
  1436. #endif
  1437. }
  1438.  
  1439. /*
  1440.  * Compare the contents of the mmap entries, and decide if we are allowed to
  1441.  * share the pages
  1442.  */
  1443. int file_mmap_share(struct vm_area_struct * area1,
  1444.             struct vm_area_struct * area2,
  1445.             unsigned long address)
  1446. {
  1447.     if (area1->vm_inode != area2->vm_inode)
  1448.         return 0;
  1449.     if (area1->vm_start != area2->vm_start)
  1450.         return 0;
  1451.     if (area1->vm_end != area2->vm_end)
  1452.         return 0;
  1453.     if (area1->vm_offset != area2->vm_offset)
  1454.         return 0;
  1455.     if (area1->vm_page_prot != area2->vm_page_prot)
  1456.         return 0;
  1457.     return 1;
  1458. }
  1459.  
  1460. struct vm_operations_struct file_mmap = {
  1461.     NULL,            /* open */
  1462.     file_mmap_free,     /* close */
  1463.     file_mmap_nopage,    /* nopage */
  1464.     NULL,            /* wppage */
  1465.     file_mmap_share,    /* share */
  1466.     NULL,            /* unmap */
  1467. };
  1468.  
  1469. /*
  1470.  * BAD_PAGE is the page that is used for page faults when linux
  1471.  * is out-of-memory. Older versions of linux just did a
  1472.  * do_exit(), but using this instead means there is less risk
  1473.  * for a process dying in kernel mode, possibly leaving a inode
  1474.  * unused etc..
  1475.  *
  1476.  * BAD_PAGETABLE is the accompanying page-table: it is initialized
  1477.  * to point to BAD_PAGE entries.
  1478.  *
  1479.  * ZERO_PAGE is a special page that is used for zero-initialized
  1480.  * data and COW.
  1481.  */
  1482. static unsigned long empty_bad_page_table;
  1483.  
  1484. unsigned long __bad_pagetable(void)
  1485. {
  1486.     int i;
  1487.  
  1488.     for( i = 0; i < PTRS_PER_PAGE; i++ )
  1489.     ((unsigned long *)empty_bad_page_table)[i] = VTOP(BAD_PAGE) | PAGE_PRIVATE;
  1490.  
  1491.     return empty_bad_page_table;
  1492. }
  1493.  
  1494. static unsigned long empty_bad_page;
  1495.  
  1496. unsigned long __bad_page(void)
  1497. {
  1498.     memset ((void *)empty_bad_page, 0, PAGE_SIZE);
  1499.  
  1500.     return empty_bad_page;
  1501. }
  1502.  
  1503. static unsigned long empty_zero_page;
  1504.  
  1505. unsigned long __zero_page(void)
  1506. {
  1507.     memset ((void *)empty_zero_page, 0, PAGE_SIZE);
  1508.  
  1509.     return empty_zero_page;
  1510. }
  1511.  
  1512. void show_mem(void)
  1513. {
  1514.     unsigned long i;
  1515.     int free = 0, total = 0, reserved = 0, nonshared = 0, shared = 0;
  1516.  
  1517.     printk("Mem-info:\n");
  1518.     printk("Free pages:      %6dkB\n",nr_free_pages<<(PAGE_SHIFT-10));
  1519.     printk("Secondary pages: %6dkB\n",nr_secondary_pages<<(PAGE_SHIFT-10));
  1520.     printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
  1521.     printk("Buffer memory:   %6dkB\n",buffermem>>10);
  1522.     printk("Buffer heads:    %6d\n",nr_buffer_heads);
  1523.     printk("Buffer blocks:   %6d\n",nr_buffers);
  1524.     i = (high_memory-0xC0000000) >> PAGE_SHIFT;
  1525.     while (i-- > 0) {
  1526.     total++;
  1527.     if (mem_map[i] & MAP_PAGE_RESERVED)
  1528.         reserved++;
  1529.     else if (!mem_map[i])
  1530.         free++;
  1531.     else if (mem_map[i] == 1)
  1532.         nonshared++;
  1533.     else
  1534.         shared++;
  1535.     }
  1536.     printk("%d pages of RAM\n",total);
  1537.     printk("%d free pages\n",free);
  1538.     printk("%d reserved pages\n",reserved);
  1539.     printk("%d pages nonshared\n",nonshared);
  1540.     printk("%d pages shared\n",shared);
  1541. }
  1542.  
  1543. /* kernel code start address */
  1544. asmlinkage void start(void);
  1545. /* "end" marks the end of the kernel text/data/bss */
  1546. extern int end;
  1547.  
  1548. /*
  1549.  * The following two routines map from a physical address to a kernel
  1550.  * virtual address and vice versa.
  1551.  */
  1552. unsigned long mm_vtop (unsigned long vaddr)
  1553. {
  1554.     int i;
  1555.     unsigned long voff = vaddr - KSTART_ADDR;
  1556.     unsigned long offset = 0;
  1557.  
  1558.     for (i = 0; i < boot_info.num_memory; i++)
  1559.     {
  1560.         if (voff < offset + boot_info.memory[i].size) {
  1561. #ifdef DEBUGPV
  1562.             printk ("VTOP(%lx)=%lx\n", vaddr,
  1563.                 boot_info.memory[i].addr + voff - offset);
  1564. #endif
  1565.             return boot_info.memory[i].addr + voff - offset;
  1566.         } else
  1567.             offset += boot_info.memory[i].size;
  1568.     }
  1569.  
  1570.     panic ("VTOP: bad virtual address %08lx", vaddr);
  1571. }
  1572.  
  1573. unsigned long mm_ptov (unsigned long paddr)
  1574. {
  1575.     int i;
  1576.     unsigned long offset = KSTART_ADDR;
  1577.  
  1578.     for (i = 0; i < boot_info.num_memory; i++)
  1579.     {
  1580.         if (paddr >= boot_info.memory[i].addr &&
  1581.             paddr < (boot_info.memory[i].addr
  1582.                  + boot_info.memory[i].size)) {
  1583. #ifdef DEBUGPV
  1584.             printk ("PTOV(%lx)=%lx\n", paddr,
  1585.                 (paddr - boot_info.memory[i].addr) + offset);
  1586. #endif
  1587.             return (paddr - boot_info.memory[i].addr) + offset;
  1588.         } else
  1589.             offset += boot_info.memory[i].size;
  1590.     }
  1591.  
  1592.     panic ("PTOV: bad physical address %08lx (called from %p)\n",
  1593.            paddr, __builtin_return_address(0));
  1594. }
  1595.  
  1596. static unsigned long *kernel_pointer_table (void)
  1597. {
  1598.     /*
  1599.      * For a pointer table for the kernel virtual address space,
  1600.      * use one of the pointer tables in the page holding the root
  1601.      * table.  This page can hold up to 8 pointer tables.  3 of
  1602.      * these tables are always reserved (root table, kernel
  1603.      * pointer table, stack pointer table).  In addition, the 4th
  1604.      * pointer table in this page is reserved.  If you are running
  1605.      * on an '040 Amiga, this pointer table is used to map in the
  1606.      * Amiga hardware.  It may be used for other purposes on other
  1607.      * 68K machines.  This leaves 4 pointer tables available for
  1608.      * use by the kernel.  This allows mapping of 5 * 32 = 160M of
  1609.      * physical memory.  This should be sufficient for now.
  1610.      */
  1611.     extern unsigned long *krt; /* address of kernel root table */
  1612.     unsigned long *ptable;
  1613.     /* index of available table */
  1614.     static int kptavail = PAGE_SIZE/(sizeof(long)*128) - 4;
  1615.  
  1616.     ptable = (unsigned long *)&krt[kptavail*128];
  1617.     kptavail++;
  1618.     if (kptavail == PAGE_SIZE/(sizeof(long)*128))
  1619.         panic ("no space for kernel pointer table");
  1620.  
  1621.     return ptable;
  1622. }
  1623.  
  1624.  
  1625. static unsigned long *kernel_page_table (unsigned long *memavailp)
  1626. {
  1627.     unsigned long *ptablep;
  1628.  
  1629.     ptablep = (unsigned long *)*memavailp;
  1630.     *memavailp += PAGE_SIZE;
  1631.  
  1632.     nocache_page ((unsigned long)ptablep);
  1633.  
  1634.     return ptablep;
  1635. }
  1636.  
  1637. static unsigned long map_chunk (unsigned long addr,
  1638.                 unsigned long size,
  1639.                 unsigned long *memavailp)
  1640. {
  1641. #define ONEMEG    (1024*1024)
  1642.  
  1643.     int is040 = (boot_info.cputype & CPU_68040);
  1644.     static unsigned long mem_mapped = 0;
  1645.     static unsigned long virtaddr = KSTART_ADDR;
  1646.     static unsigned long *ktablep = NULL;
  1647.     unsigned long *kpointerp, physaddr;
  1648.     extern unsigned long *krt, *kpt;
  1649.     int pindex;   /* index into pointer table */
  1650.  
  1651.     kpointerp = (unsigned long *)(krt[L1_INDEX(virtaddr)] & 0xfffffff0);
  1652.     if (kpointerp)
  1653.         kpointerp = (unsigned long *)PTOV(kpointerp);
  1654.  
  1655.     /*
  1656.      * pindex is the offset into the pointer table for the
  1657.      * descriptors for the current virtual address being mapped.
  1658.      */
  1659.     pindex = (virtaddr >> 18) & 0x7f;
  1660.  
  1661. #ifdef DEBUG
  1662.   printk ("map_chunk: adr=%#lx size=%#lx\n",addr,size);
  1663.     printk ("mm=%ld, krt=%p, kpointerp=%p, pindex=%d\n", mem_mapped,
  1664.         krt, kpointerp, pindex);
  1665. #endif
  1666.  
  1667.     /*
  1668.      * if this is running on an '040, we already allocated a page
  1669.      * table for the first 4M.  The address is stored in kpt by
  1670.      * arch/head.S
  1671.      *
  1672.      */
  1673.     if (is040 && mem_mapped == 0)
  1674.         ktablep = kpt;
  1675.  
  1676.     for (physaddr = addr;
  1677.          physaddr < addr + size;
  1678.          mem_mapped += ONEMEG, virtaddr += ONEMEG) {
  1679.  
  1680. #ifdef DEBUG
  1681.         printk ("pa=%#lx va=%#lx ", physaddr, virtaddr);
  1682. #endif
  1683.  
  1684.         if (pindex > 128 && mem_mapped >= 32*ONEMEG) {
  1685.             /* we need a new pointer table every 32M */
  1686. #ifdef DEBUG
  1687.             printk ("[new pointer]");
  1688. #endif
  1689.  
  1690.             kpointerp = kernel_pointer_table ();
  1691.             krt[L1_INDEX(addr)] = VTOP(kpointerp) | PAGE_TABLE;
  1692.             pindex = 0;
  1693.         }
  1694.  
  1695.             /*
  1696.              * 68030, use early termination page descriptors.
  1697.              * Each one points to 64 pages (256K).  Using 4
  1698.              * page descriptors results in a mapping of 1M
  1699.              */
  1700. #ifdef DEBUG
  1701.             printk ("[early term] ");
  1702. #endif
  1703.  
  1704.             kpointerp[pindex++] = physaddr | PAGE_PRESENT;
  1705. #ifdef DEBUG
  1706.             printk ("%lx=%lx ", VTOP(&kpointerp[pindex-1]),
  1707.                 kpointerp[pindex-1]);
  1708. #endif
  1709.             physaddr += 64 * 4096;
  1710.             kpointerp[pindex++] = physaddr | PAGE_PRESENT;
  1711. #ifdef DEBUG
  1712.             printk ("%lx=%lx ", VTOP(&kpointerp[pindex-1]),
  1713.                 kpointerp[pindex-1]);
  1714. #endif
  1715.             physaddr += 64 * 4096;
  1716.             kpointerp[pindex++] = physaddr | PAGE_PRESENT;
  1717. #ifdef DEBUG
  1718.             printk ("%lx=%lx ", VTOP(&kpointerp[pindex-1]),
  1719.                 kpointerp[pindex-1]);
  1720. #endif
  1721.             physaddr += 64 * 4096;
  1722.             kpointerp[pindex++] = physaddr | PAGE_PRESENT;
  1723. #ifdef DEBUG
  1724.             printk ("%lx=%lx ", VTOP(&kpointerp[pindex-1]),
  1725.                 kpointerp[pindex-1]);
  1726. #endif
  1727.             physaddr += 64 * 4096;
  1728.  
  1729. #ifdef DEBUG
  1730.         printk ("\n");
  1731. #endif
  1732.     }
  1733.  
  1734.     return mem_mapped;
  1735. }
  1736.  
  1737. #define    clear040(paddr) __asm__ __volatile__ ("movel %0,a0\n\t"\
  1738.                           ".word 0xf4d0"\
  1739.                           /* CINVP I/D (a0) */\
  1740.                           : : "g" ((paddr))\
  1741.                           : "a0")
  1742.  
  1743. #define    push040(paddr) __asm__ __volatile__ ("movel %0,a0\n\t"\
  1744.                          ".word 0xf4f0"\
  1745.                          /* CPUSHP I/D (a0) */\
  1746.                          : : "g" ((paddr))\
  1747.                          : "a0")
  1748.  
  1749. #define    pushv040(vaddr) { unsigned long paddr;\
  1750.               __asm__ __volatile__ ("movel %1,a0\n\t"\
  1751.                         /* ptestr (a0) */\
  1752.                         ".word 0xf568\n\t"\
  1753.                         /* movec mmusr,d0 */\
  1754.                         ".long 0x4e7a0805\n\t"\
  1755.                         "andw #0xf000,d0\n\t"\
  1756.                         "movel d0,%0"\
  1757.                         : "=g" (paddr)\
  1758.                         : "g" ((vaddr))\
  1759.                         : "a0", "d0");\
  1760.               push040(paddr); }
  1761.  
  1762.  
  1763. /*
  1764.  * 040: Hit every page containing an address in the range paddr..paddr+len-1.
  1765.  * (Low order bits of the ea of a CINVP/CPUSHP are "don't care"s).
  1766.  * Hit every page until there is a page or less to go. Hit the next page,
  1767.  * and the one after that if the range hits it.
  1768.  */
  1769. void cache_clear (unsigned long paddr, int len)
  1770. {
  1771.     if (boot_info.cputype & CPU_68040) {
  1772.     /* on 68040, invalidate cache lines for pages in the range */
  1773.     while (len > PAGE_SIZE) {
  1774.         clear040(paddr);
  1775.         len -= PAGE_SIZE;
  1776.         paddr += PAGE_SIZE;
  1777.         }
  1778.     if (len > 0) {
  1779.         /* 0 < len <= PAGE_SIZE */
  1780.         clear040(paddr);
  1781.         if (((paddr + len - 1) / PAGE_SIZE) != (paddr / PAGE_SIZE)) {
  1782.         /* a page boundary gets crossed at the end */
  1783.         clear040(paddr + len - 1);
  1784.         }
  1785.         }
  1786.     }
  1787.     else /* 68030 or 68020 */
  1788.     asm volatile ("movec cacr,d0\n\t"
  1789.               "oriw %0,d0\n\t"
  1790.               "movec d0,cacr"
  1791.               : : "i" (FLUSH_I_AND_D)
  1792.               : "d0");
  1793.     }
  1794.  
  1795.  
  1796.  
  1797. void cache_push (unsigned long paddr, int len)
  1798. {
  1799.     if (boot_info.cputype & CPU_68040) {
  1800.     /* on 68040, push cache lines for pages in the range */
  1801.     while (len > PAGE_SIZE) {
  1802.         push040(paddr);
  1803.         len -= PAGE_SIZE;
  1804.         paddr += PAGE_SIZE;
  1805.         }
  1806.     if (len > 0) {
  1807.         push040(paddr);
  1808.         if (((paddr + len - 1) / PAGE_SIZE) != (paddr / PAGE_SIZE)) {
  1809.         /* a page boundary gets crossed at the end */
  1810.         push040(paddr);
  1811.         }
  1812.         }
  1813.     }
  1814.     
  1815.     
  1816.     /*
  1817.      * 68030/68020 have no writeback cache. On the other hand,
  1818.      * cache_push is actually a superset of cache_clear (the lines
  1819.      * get written back and invalidated), so we should make sure
  1820.      * to perform the corresponding actions. After all, this is getting
  1821.      * called in places where we've just loaded code, or whatever, so
  1822.      * flushing the icache is appropriate; flushing the dcache shouldn't
  1823.      * be required.
  1824.      */
  1825.     else /* 68030 or 68020 */
  1826.     asm volatile ("movec cacr,d0\n\t"
  1827.               "oriw %0,d0\n\t"
  1828.               "movec d0,cacr"
  1829.               : : "i" (FLUSH_I)
  1830.               : "d0");
  1831.     }
  1832.  
  1833. void cache_push_v (unsigned long vaddr, int len)
  1834. {
  1835.     if (boot_info.cputype & CPU_68040) {
  1836.     /* on 68040, push cache lines for pages in the range */
  1837.     while (len > PAGE_SIZE) {
  1838.         pushv040(vaddr);
  1839.         len -= PAGE_SIZE;
  1840.         vaddr += PAGE_SIZE;
  1841.         }
  1842.     if (len > 0) {
  1843.         pushv040(vaddr);
  1844.         if (((vaddr + len - 1) / PAGE_SIZE) != (vaddr / PAGE_SIZE)) {
  1845.         /* a page boundary gets crossed at the end */
  1846.         pushv040(vaddr);
  1847.         }
  1848.         }
  1849.     }
  1850.     /* 68030/68020 have no writeback cache; still need to clear icache. */
  1851.     else /* 68030 or 68020 */
  1852.     asm volatile ("movec cacr,d0\n\t"
  1853.               "oriw %0,d0\n\t"
  1854.               "movec d0,cacr"
  1855.               : : "i" (FLUSH_I)
  1856.               : "d0");
  1857. }
  1858.  
  1859. #undef clear040
  1860. #undef push040
  1861. #undef pushv040
  1862.  
  1863. /*
  1864.  * Bits to add to page descriptors for "normal" caching mode.
  1865.  * For 68020/030 this is 0.
  1866.  * For 68040, this is PAGE_CACHE040 (cachable, copyback)
  1867.  */
  1868. unsigned long mm_cachebits;
  1869.  
  1870. unsigned long interrupt_stack[1024];
  1871.  
  1872. /*
  1873.  * paging_init() continues the virtual memory environment setup which
  1874.  * was begun by the code in arch/head.S.
  1875.  * The sole parameter is the starting address of available memory.
  1876.  */
  1877. void paging_init(unsigned long *startp,
  1878.          unsigned long *endp)
  1879. {
  1880.     int chunk;
  1881.     unsigned long virtual_start, virtual_end;
  1882.     unsigned long mem_avail = 0;
  1883.     /* pointer to page table for kernel stacks */
  1884.     extern unsigned long *krt, availmem;
  1885.  
  1886.     /*
  1887.      * Setup cache bits
  1888.      */
  1889.     mm_cachebits = boot_info.cputype & CPU_68040 ? PAGE_CACHE040 : 0;
  1890.  
  1891.     /*
  1892.      * Indicate that the 680x0 can handle write protect faults
  1893.      * from kernel mode.
  1894.      */
  1895.     wp_works_ok = 1;
  1896.  
  1897.     /*
  1898.      * Map the physical memory available into the kernel virtual
  1899.      * address space.  It may allocate some memory for page
  1900.      * tables and thus modify availmem.
  1901.      */
  1902.  
  1903.     for (chunk = 0; chunk < boot_info.num_memory; chunk++)
  1904.         mem_avail = map_chunk (boot_info.memory[chunk].addr,
  1905.                        boot_info.memory[chunk].size,
  1906.                        &availmem);
  1907.                        
  1908.  
  1909. #ifdef DEBUG
  1910.     printk ("memory available is %ldKB\n", mem_avail >> 10);
  1911. #endif
  1912.  
  1913.     /*
  1914.      * virtual address after end of kernel
  1915.      * "availmem" is setup by the code in head.S.
  1916.      */
  1917.     virtual_start = availmem;
  1918.  
  1919.     /* virtual address of end of available memory */
  1920.     virtual_end = (unsigned long)start + mem_avail;
  1921.  
  1922. #ifdef DEBUG
  1923.     printk ("virtual_start is %#lx\nvirtual_end is %#lx\n",
  1924.         virtual_start, virtual_end);
  1925. #endif
  1926.  
  1927.     /*
  1928.      * initialize the bad page table and bad page to point
  1929.      * to a couple of allocated pages
  1930.      */
  1931.     empty_bad_page_table = virtual_start;
  1932.     virtual_start += PAGE_SIZE;
  1933.     empty_bad_page = virtual_start;
  1934.     virtual_start += PAGE_SIZE;
  1935.     empty_zero_page = virtual_start;
  1936.     virtual_start += PAGE_SIZE;
  1937.  
  1938.     /* record in task 0 (swapper) tss */
  1939.     task[0]->tss.pagedir_v = krt;
  1940.     task[0]->tss.pagedir_p = VTOP (krt);
  1941.  
  1942. #ifdef DEBUG
  1943.     printk ("task 0 pagedir at %p virt, %#lx phys\n",
  1944.         task[0]->tss.pagedir_v, task[0]->tss.pagedir_p);
  1945. #endif
  1946.  
  1947.     /* setup CPU root pointer for swapper task */
  1948.     task[0]->tss.crp[0] = 0x80000000 | PAGE_SHORT;
  1949.     task[0]->tss.crp[1] = task[0]->tss.pagedir_p;
  1950.  
  1951.     if (boot_info.cputype & CPU_68040)
  1952.         asm ("movel %0,d0\n\t"
  1953.              ".long 0x4e7b0806" /* movec d0,urp */
  1954.              : /* no outputs */
  1955.              : "g" (task[0]->tss.crp[1])
  1956.              : "d0");
  1957.     else
  1958.         asm ("pmove %0@,crp"
  1959.              : /* no outputs */
  1960.              : "a" (task[0]->tss.crp));
  1961.  
  1962. #ifdef DEBUG
  1963.     printk ("set crp\n");
  1964. #endif
  1965.  
  1966.     /*
  1967.      * Setup interrupt stack pointer
  1968.      */
  1969.     asm ("movec %0,isp" : : "r" ((unsigned long)interrupt_stack+PAGE_SIZE));
  1970.  
  1971. #ifdef DEBUG
  1972.     printk ("setup istack (%lx)\n", (unsigned long)interrupt_stack);
  1973. #endif
  1974.  
  1975.     /*
  1976.      * Set up SFC/DFC registers (user data space)
  1977.      */
  1978.     set_fs (USER_DATA);
  1979.  
  1980.     *startp = virtual_start;
  1981.     *endp = virtual_end;
  1982. }
  1983.  
  1984. void mem_init(unsigned long low_mem_init,
  1985.           unsigned long start_mem, unsigned long end_mem)
  1986. {
  1987.     int codepages = 0;
  1988.     int reservedpages = 0;
  1989.     int datapages = 0;
  1990.     unsigned long tmp;
  1991.     unsigned short * p;
  1992.     extern int etext;
  1993.  
  1994.     end_mem &= PAGE_MASK;
  1995.     high_memory = end_mem;
  1996.     start_mem += 0x0000000f;
  1997.     start_mem &= ~0x0000000f;
  1998.  
  1999.     tmp = MAP_NR(end_mem);
  2000.     mem_map = (unsigned short *) start_mem;
  2001.  
  2002.     p = mem_map + tmp;
  2003.     start_mem = (unsigned long) p;
  2004.     while (p > mem_map)
  2005.         *--p = MAP_PAGE_RESERVED;
  2006.     start_mem = PAGE_ALIGN(start_mem);
  2007.     while (start_mem < end_mem) {
  2008.         mem_map[MAP_NR(start_mem)] = 0;
  2009.         start_mem += PAGE_SIZE;
  2010.     }
  2011.  
  2012. #ifdef DEBUG
  2013.     printk ("task[0] root table is %p\n", task[0]->tss.pagedir_v);
  2014. #endif
  2015.  
  2016.     free_page_list = 0;
  2017.     nr_free_pages = 0;
  2018.     for (tmp = KSTART_ADDR ; tmp < end_mem ; tmp += PAGE_SIZE) {
  2019.         if (mem_map[MAP_NR(tmp)]) {
  2020.             if (tmp >= 0xA0000 && tmp < 0x100000)
  2021.                 reservedpages++;
  2022.             else if (tmp < (unsigned long)&etext)
  2023.                 codepages++;
  2024.             else
  2025.                 datapages++;
  2026.             continue;
  2027.         }
  2028.         *(unsigned long *) tmp = free_page_list;
  2029.         free_page_list = tmp;
  2030.         nr_free_pages++;
  2031.     }
  2032.     tmp = nr_free_pages << PAGE_SHIFT;
  2033.     printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data)\n",
  2034.            tmp >> 10,
  2035.            (end_mem-KSTART_ADDR) >> 10,
  2036.            codepages << (PAGE_SHIFT-10),
  2037.            reservedpages << (PAGE_SHIFT-10),
  2038.            datapages << (PAGE_SHIFT-10));
  2039. }
  2040.  
  2041. unsigned long mm_phys_to_virt (unsigned long addr)
  2042. {
  2043.     return PTOV (addr);
  2044. }
  2045.  
  2046. void si_meminfo(struct sysinfo *val)
  2047. {
  2048.     unsigned long i;
  2049.  
  2050.     i = (high_memory - KSTART_ADDR) >> PAGE_SHIFT;
  2051.     val->totalram = 0;
  2052.     val->freeram = 0;
  2053.     val->sharedram = 0;
  2054.     val->bufferram = buffermem;
  2055.     while (i-- > 0) {
  2056.     if (mem_map[i] & MAP_PAGE_RESERVED)
  2057.         continue;
  2058.     val->totalram++;
  2059.     if (!mem_map[i]) {
  2060.         val->freeram++;
  2061.         continue;
  2062.     }
  2063.     val->sharedram += mem_map[i]-1;
  2064.     }
  2065.     val->totalram <<= PAGE_SHIFT;
  2066.     val->freeram <<= PAGE_SHIFT;
  2067.     val->sharedram <<= PAGE_SHIFT;
  2068.     return;
  2069. }
  2070.  
  2071. /*
  2072.  * abstract out the high memory variable
  2073.  */
  2074. int valid_addr(unsigned long addr, int count)
  2075. {
  2076. #ifdef CONFIG_AMIGA
  2077.     if (addr < boot_info.bi_amiga.chip_size)
  2078.     /* check for chip memory */
  2079.     if (addr + count > boot_info.bi_amiga.chip_size)
  2080.         return boot_info.bi_amiga.chip_size - addr;
  2081.     else
  2082.         return count;
  2083. #endif
  2084.  
  2085.     /* otherwise, kernel virtual memory */
  2086.     if (addr >= KSTART_ADDR && addr < high_memory)
  2087.     if (addr + count > high_memory)
  2088.         return high_memory - addr;
  2089.     else
  2090.         return count;
  2091.  
  2092.     /* not valid memory */
  2093.     return 0;
  2094. }
  2095.